-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
121 lines (103 loc) · 3.63 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
from __future__ import division
import time
import os
import logging
import mercurial.commands
import mercurial.error
import mercurial.hg
import mercurial.ui
from time import strftime
import subprocess
from ConfigParser import ConfigParser
from random import Random
DOCKERFILE_PATH=os.path.join(os.path.dirname(__file__),"files/dockerfiles")
def timestamp_to_local(timestamp):
x=time.localtime(timestamp)
time_format='%Y-%m-%d %H:%M:%S'
return time.strftime(time_format,x)
def byte_to_gb(byte):
_x = eval('{}/1000/1000/1000'.format(byte))
print _x
_y = eval('1577283853/1000/1000/1000')
print _y
return "%.2fGB" % _x
def human_readable_size(size):
import humanize
return humanize.naturalsize(size)
def random_str(randomlength=8):
str = ''
#chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789'
chars = 'abcdefghijklmnopqrstuvwxyz0123456789'
length = len(chars) - 1
random = Random()
for i in range(randomlength):
str += chars[random.randint(0, length)]
return str
def create_file(repo_path,content):
#_path=os.path.join(DOCKERFILE_PATH,name)
#_path="{}/{}".format(DOCKERFILE_PATH,name)
#print _path
#if not os.path.exists(_path):
# os.mkdir(_path)
file_path=os.path.join(repo_path,"Dockerfile")
with open(file_path,'w') as f:
f.write(content)
#print file_path
#_file=open(file_path,'w')
#return _file
#def create_repos(name):
# pass
def get_file_path(file_name):
import os
real_name='Dockerfile'
_path="{}/{}".format(DOCKERFILE_PATH,file_name)
return os.path.join(_path,real_name)
def get_repo_path(repo_name):
_path="{}/{}".format(DOCKERFILE_PATH,repo_name)
return _path
#def write_file(fd,content):
# fd.write(content)
# fd.close()
def get_file_size(file_path):
import os
size=os.path.getsize(file_path)
return human_readable_size(size)
def get_current_datatime():
import time
time_str=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
return time_str
class MercurialRevisionControl(object):
def __init__(self,repo_path=None,local_path=None,**options):
self._ui = mercurial.ui.ui()
self.repo_path = repo_path
def create_repo(self,path):
try:
mercurial.commands.init(self._ui, dest=path)
except Exception, e:
logging.error('Could not create Mercurial repository in %s. %s: %s',
path, e.__class__.__name__, str(e))
def add(self,repo_path):
repo=mercurial.hg.repository(self._ui,repo_path)
mercurial.commands.add(self._ui,repo=repo)
def commit(self,repo_path):
timestamp=strftime('%Y-%m-%d %H:%M:%S')
message = "jaeweb auto-commit:%s" % timestamp
self._ui.pushbuffer()
repo=mercurial.hg.repository(self._ui,repo_path)
mercurial.commands.commit(self._ui,repo=repo,message=message,logfile=None,addremove=None,user=None,date=None)
logging.debug('doing commit at %s' % repo_path)
logging.debug(self._ui.popbuffer())
def hg_rc(self,repo_path,section,option,value):
hg_rc="{}/.hg/hgrc".format(repo_path)
if not os.path.isfile(hg_rc):
subprocess.call(['touch',hg_rc])
parser=ConfigParser()
parser.read(hg_rc)
if not parser.has_section(section):
with open(hg_rc,'a') as f:
f.write('[%s]\n' % section)
if not parser.has_option(section,option):
with open(hg_rc,'a') as f:
f.write('%s = %s\n' % (option,value))
if __name__ == '__main__':
print byte_to_gb('1577283853')