示例#1
0
 def save(clazz, filename, content=None, mode=None, codec='utf-8'):
     'Atomically save content to filename using an intermediate temporary file.'
     dirname, basename = os.path.split(filename)
     dirname = dirname or None
     if dirname:
         clazz.mkdir(path.dirname(filename))
     if path.exists(filename):
         clazz.remove(filename)
     tmp = tempfile.NamedTemporaryFile(prefix=basename,
                                       dir=dirname,
                                       delete=False,
                                       mode='w+b')
     if content:
         if compat.IS_PYTHON3 and string_util.is_string(
                 content) and codec is not None:
             content_data = codecs.encode(content, codec)
         else:
             content_data = content
         tmp.write(content_data)
     tmp.flush()
     os.fsync(tmp.fileno())
     tmp.close()
     if mode:
         os.chmod(tmp.name, mode)
     clazz._cross_device_safe_rename(tmp.name, filename)
     return filename
示例#2
0
 def parse_file_type(clazz, s):
     if not string_util.is_string(s):
         return None
     t = clazz._NAME_TO_TYPE.get(s, None)
     if t:
         return t
     return clazz._SHORT_NAME_TO_TYPE.get(s, None)
示例#3
0
 def split(clazz, p):
     'Split a path.'
     if not p:
         return []
     if p == '/':
         return ['']
     p = path.normpath(p)
     assert string_util.is_string(p)
     return p.split(os.sep)
示例#4
0
 def mask_is_valid(clazz, mask):
     if not string_util.is_string(mask):
         return False
     #parts = clazz.mask_split(mask)
     parts = clazz._resolve_mask_to_list(mask)
     for part in parts:
         if not clazz.system_is_valid(part):
             return False
     return True
示例#5
0
 def parse_item_type(clazz, s):
     if isinstance(s, temp_content):
         return s
     elif string_util.is_string(s):
         s = s.lower()
         if s in ['file', 'f']:
             return clazz.FILE
         elif s in ['dir', 'd']:
             return clazz.DIR
     return None
示例#6
0
 def parse(clazz, item):
     if string_util.is_string(item):
         return clazz._parse_string(item)
     elif isinstance(item, list):
         return clazz._parse_tuple(tuple(item))
     elif isinstance(item, tuple):
         return clazz._parse_tuple(item)
     else:
         raise TypeError('not a valid temp item: %s - %s' %
                         (str(item), type(item)))
示例#7
0
 def _resolve_mask_part(clazz, part):
     assert string_util.is_string(part)
     assert clazz.DELIMITER not in part
     part = clazz.ALIASES.get(part, part)
     if clazz.DELIMITER in part:
         return clazz._resolve_mask_to_list(part)
     if part == clazz.NONE:
         return []
     elif part == clazz.ALL:
         return clazz.SYSTEMS
     else:
         return [part]
示例#8
0
 def _resolve_mask_to_list(clazz, s):
     assert string_util.is_string(s)
     s = s.lower()
     s = clazz.ALIASES.get(s, s)
     parts = [part for part in clazz.mask_split(s) if part]
     result = []
     for part in parts:
         result.extend(clazz._resolve_mask_part(part))
     result = sorted(algorithm.unique(result))
     if not result:
         return [clazz.NONE]
     return result
示例#9
0
 def _resolve_deps(clazz, dep_map, name):
   'Return a set of resolved dependencies for the given name.  Not in build order.'
   assert string_util.is_string(name)
   if name not in dep_map:
     raise missing_dependency_error('Missing dependency for %s: %s' % (clazz._dependent_for_name(dep_map, name), name), [ name ])
   result = set()
   deps = dep_map[name]
   assert isinstance(deps, set)
   result |= deps
   for dep in deps:
     result |= clazz._resolve_deps(dep_map, dep)
   return result
示例#10
0
 def _upstream_version_is_valid(clazz, upstream_version):
   if not string_util.is_string(upstream_version):
     return clazz._validation(False, 'invalid upstream_version of type %s - should be string: \"%s\"' % (type(upstream_version), str(upstream_version)))
   if upstream_version == None:
     return clazz._validation(False, 'upstream_version is None')
   if upstream_version == '':
     return clazz._validation(False, 'upstream_version is empty')
   for c in upstream_version:
     if not clazz._upstream_char_is_valid(c):
       return clazz._validation(False, 'invalid character \"%s\" in upstream_version \"%s\"' % (c, upstream_version))
   if not upstream_version[0].isalnum():
     return clazz._validation(False, 'upstream_version should start with a alphanumeric: \"%s\"' % (upstream_version))
   return clazz._validation(True, None)
示例#11
0
 def parse_requirements(clazz, requirements):
     requirements = requirements or []
     result = []
     for dep in requirements:
         if check.is_requirement(dep):
             result.append(dep)
         elif string_util.is_string(dep):
             reqs = requirement_list.parse(dep)
             result.extend(reqs)
         else:
             raise RuntimeError('Invalid requirement: %s - %s' %
                                (str(dep), type(dep)))
     return requirement_list(result)
示例#12
0
 def search_string(clazz,
                   content,
                   patterns,
                   word_boundary=False,
                   word_boundary_chars=None):
     assert string_util.is_string(content)
     patterns = object_util.listify(patterns)
     result = []
     original_patterns = None
     if word_boundary:
         original_patterns = patterns[:]
         patterns = [clazz._make_expresion(p) for p in patterns]
     original_patterns = original_patterns or patterns
     patterns = list(zip(patterns, original_patterns))
     for line_number, line in enumerate(content.splitlines(), 1):
         if word_boundary:
             result += clazz._search_line_with_re(line, patterns,
                                                  '<unknown>', line_number)
         else:
             result += clazz._search_line_with_find(line, patterns,
                                                    '<unknown>',
                                                    line_number)
     return result
示例#13
0
 def parse_mode(clazz, m):
     if string_util.is_string(m):
         return int(m, 8)
     if isinstance(m, int):
         return m
     return None
示例#14
0
 def _parse_string(clazz, s):
     assert string_util.is_string(s)
     parts = string_list_parser.parse_to_list(s)
     return clazz._parse_tuple(tuple(parts))
示例#15
0
 def unquote_strings(self):
     for i, kv in enumerate(self._values):
         if string_util.is_string(kv.value):
             self._values[i] = key_value(kv.key,
                                         string_util.unquote(kv.value))
示例#16
0
 def test_is_string(self):
     self.assertEqual(True, string_util.is_string('foo'))
     self.assertEqual(True, string_util.is_string(u'foo'))
     self.assertEqual(True, string_util.is_string(r'foo'))
     self.assertEqual(False, string_util.is_string(['foo']))
     self.assertEqual(False, string_util.is_string(False))
示例#17
0
 def name_is_valid(clazz, name):
     if not string_util.is_string(name):
         return False
     if not name:
         return False
     return True