-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
74 lines (48 loc) · 1.55 KB
/
Copy pathutils.py
File metadata and controls
74 lines (48 loc) · 1.55 KB
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
# (c) 2010-2011 by Anton Korenyushkin
from __future__ import with_statement
import re
import fcntl
from django.db import connection
from error import Error
NAME_RE = re.compile('^%(w)s(?:%(w)s|-%(w)s)*$' % {'w': '[a-zA-Z0-9]'})
MAX_NAME_LEN = 30
NAME_PATTERN = '[a-zA-Z0-9-]{1,%d}' % MAX_NAME_LEN
def check_name(name):
if not NAME_RE.match(name):
raise Error(
'The name "%s" is incorrect.' % name,
('Name must consist of Latin letters, digits, and single hyphens.' +
'It must not start or end with a hyphen.'))
if len(name) > MAX_NAME_LEN:
raise Error(
'The name is too long.',
'Name length must not exceed %d characters.' % MAX_NAME_LEN)
def read_file(path):
with open(path) as f:
return f.read()
def write_file(path, data):
with open(path, 'w') as f:
f.write(data)
def touch_file(path):
open(path, 'w').close()
def get_id(dev_name, app_name, env_name=None):
result = dev_name + ':' + app_name
if env_name:
result += ':' + env_name
return result.lower()
def execute_sql(query, params=()):
connection.cursor().execute(query, params)
class BaseLock(object):
def __init__(self, path):
self._file = open(path)
fcntl.flock(self._file, self.kind)
def release(self):
self._file.close()
def __enter__(self):
pass
def __exit__(self, cls, value, traceback):
self.release()
class SharedLock(BaseLock):
kind = fcntl.LOCK_SH
class ExclusiveLock(BaseLock):
kind = fcntl.LOCK_EX