Esempio n. 1
0
    def sandbox_file_line_match(self, match, line):
        """ Map content of file to self.dic['__files'][name].

            Raise from loader.exceptions:
                - SyntaxErrorPL if no group 'file' was found
                - DirectoryNotFound if trying to load from a nonexistent directory
                - FileNotFound if the given file do not exists."""

        try:
            directory, path = get_location(self.directory,
                                           match.group('file'),
                                           current=dirname(self.path))
            name = basename(path) if not match.group('alias') else match.group(
                'alias')

            self.dic['__dependencies'].append(path)
            with open(join(settings.FILEBROWSER_ROOT, directory, path)) as f:
                self.dic['__files'][name] = f.read()

        except FileNotFoundError as e:
            raise FileNotFound(self.path_parsed_file, line,
                               match.group('file'), self.lineno, str(e))
        except SyntaxError as e:
            raise SyntaxErrorPL(self.path_parsed_file, line, self.lineno,
                                str(e))
Esempio n. 2
0
    def from_file_line_match(self, match, line):
        """ Map (or append) the content if the file corresponding to file)
            to the key

            Raise from loader.exceptions:
                - SyntaxErrorPL if no group 'file' or 'key' was found
                - SemanticError if trying to append a nonexistent key
                - DirectoryNotFound if trying to load from a nonexistent directory
                - FileNotFound if the given file do not exists."""
        
        key = match.group('key')
        op = match.group('operator')
        
        try:
            directory, path = get_location(self.directory, match.group('file'),
                                           current=dirname(self.path), parser=self)
            
            with open(join(settings.FILEBROWSER_ROOT, directory, path)) as f:
                if '+' in op:
                    self.dic_add_key(key, f.read(), append=True)
                elif '-' in op:
                    self.dic_add_key(key, f.read(), prepend=True)
                else:
                    self.dic_add_key(key, f.read())
        except FileNotFoundError as e:
            raise FileNotFound(self.path_parsed_file, line, match.group('file'), self.lineno,
                               str(e))
        except SyntaxError as e:
            raise SyntaxErrorPL(self.path_parsed_file, line, self.lineno, str(e))
Esempio n. 3
0
    def extends_line_match(self, match, line):
        """ Appends file, line and lineno to self.dic['__extends'] so that it can be later processed
            by loader.parser.

            Raise from loader.exceptions
                - SyntaxErrorPL if no group 'file' was found.
                - DirectoryNotFound if the directory indicated by the pl couldn't be found"""

        try:
            directory, path = get_location(self.directory,
                                           match.group('file'),
                                           current=dirname(self.path))
        except SyntaxError as e:
            raise SyntaxErrorPL(self.path_parsed_file, line, self.lineno,
                                str(e))
        except FileNotFoundError as e:
            raise FileNotFound(self.path_parsed_file, line,
                               match.group('file'), self.lineno, str(e))

        self.dic['__extends'].append({
            'path': path,
            'line': line,
            'lineno': self.lineno,
            'directory_name': directory
        })
Esempio n. 4
0
def load_PLTP(directory, rel_path, force=False):
    """ Load the given file as a PLTP. Save it and its PL in the database.
        
        Return:
            - (PLTP, []) if the PLTP was loaded successfully
            - (PLTP, warning_list) if the PLTP was loaded with warnings
            - (None, None) if PLTP is already loaded
    """

    name = splitext(basename(rel_path))[0]

    sha1 = hashlib.sha1()
    sha1.update((directory.name + ':' + rel_path).encode('utf-8'))
    sha1 = sha1.hexdigest()

    try:
        existing = PLTP.objects.get(sha1=sha1)
        if not force:
            return None, None
        existing.delete()  # Delete the current PLTP entry if force is False
    except:  # If the PLTP does not exist, keep going
        pass

    dic, warnings = parse_file(directory, rel_path)

    pl_list = list()
    for item in dic['__pl']:
        try:
            pl_directory = Directory.objects.get(name=item['directory_name'])
        except ObjectDoesNotExist:
            raise DirectoryNotFound(dic['__rel_path'], item['line'],
                                    item['path'], item['lineno'])
        pl_directory, pl_path = get_location(directory, item['path'])
        pl, pl_warnings = load_PL(pl_directory, pl_path)
        warnings += pl_warnings
        pl_list.append(pl)

    for pl in pl_list:
        pl.save()
        logger.info("PL '" + str(pl.id) + " (" + pl.name +
                    ")' has been added to the database")

    pltp = PLTP(name=name,
                sha1=sha1,
                json=dic,
                directory=directory,
                rel_path=rel_path)
    pltp.save()
    logger.info("PLTP '" + sha1 + " (" + name +
                ")' has been added to the database")

    for pl in pl_list:
        pltp.pl.add(pl)

    return pltp, [exception_to_html(warning) for warning in warnings]
Esempio n. 5
0
    def from_file_line_match(self, match, line):
        """ Map (or append) the content if the file corresponding to file)
            to the key
            
            Raise from loader.exceptions:
                - SyntaxErrorPL if no group 'file' or 'key' was found
                - SemanticError if trying to append a nonexistent key
                - DirectoryNotFound if trying to load from a nonexistent directory
                - FileNotFound if the given file do not exists."""

        if not match.group('file') or not match.group(
                'key') or not match.group('operator'):
            raise SyntaxErrorPL(self.path_parsed_file, line, self.lineno)

        key = match.group('key')
        op = match.group('operator')

        # Add warning when overwritting a key
        if key in self.dic and '+' not in op:
            self.add_warning("Key '" + key + "' overwritten at line " +
                             str(self.lineno))

        try:
            directory, path = get_location(self.directory,
                                           match.group('file'),
                                           current=self.path_parsed_file)
            path = abspath(
                join(directory.root, path.replace(directory.name + '/', '')))
            with open(path, 'r') as f:
                if '+' in op:
                    if not key in self.dic:
                        raise SemanticError(
                            self.path_parsed_file, line, self.lineno,
                            "Trying to append to non-existent key '" + key +
                            "'.")
                    self.dic[key] += f.read()
                else:
                    self.dic[key] = f.read()
        except ObjectDoesNotExist:
            raise DirectoryNotFound(self.path_parsed_file, line,
                                    match.group('file'), self.lineno)
        except FileNotFoundError:
            raise FileNotFound(self.path_parsed_file,
                               line,
                               path,
                               lineno=self.lineno)
        except ValueError:
            raise FileNotFound(
                self.path_parsed_file,
                line,
                match.group('file'),
                lineno=self.lineno,
                message="Path from another directory must be absolute")
Esempio n. 6
0
    def abspath(self, ref):
        """
            Converts the given path 'ref' to an absolute path in the file system.

            :param ref: the path to resolve relative to ``self.relative``
            :return: the absolute path of the resolved reference.
        """
        
        directory, path = get_location(self.directory, ref.strip(),
                                       current=dirname(self.relative))
        path = join_fb_root(join(directory, path))
        return path
Esempio n. 7
0
    def test_get_location(self):
        current_dir = Directory.objects.get(name='dir1')
        current_path = 'first1/second1/'

        # Correct absolute path to current directory
        path1 = '/first1/second1/third1/file.py'
        # Correct absolute path to current directory
        path2 = 'dir1:/first1/second1/third1/file.py'
        # Correct absolute path to another directory
        path3 = 'dir2:/first1/second1/third1/file.py'
        # Correct relative path to current directory
        path4 = '../second2/third2/file.py'
        # Correct relative path to current directory
        path5 = 'file.py'
        # dir3 does not exists, raise ObjectDoesNotExist
        path6 = 'dir3:/first1/second1/third?/file.py1'
        # Path to another directory is not absolute, raise ValueError
        path7 = 'dir1:first1/second1/third?/file.py1'

        self.assertEqual((current_dir, 'first1/second1/third1/file.py'),
                         get_location(current_dir, path1))
        self.assertEqual((current_dir, 'first1/second1/third1/file.py'),
                         get_location(current_dir, path2))
        self.assertEqual((Directory.objects.get(name='dir2'),
                          'first1/second1/third1/file.py'),
                         get_location(current_dir, path3))
        self.assertEqual((current_dir, 'first1/second2/third2/file.py'),
                         get_location(current_dir, path4, current_path))
        self.assertEqual((current_dir, 'first1/second1/file.py'),
                         get_location(current_dir, path5, current_path))

        with self.assertRaises(ObjectDoesNotExist):
            get_location(current_dir, path6)

        with self.assertRaises(ValueError):
            get_location(current_dir, path7)
Esempio n. 8
0
    def pl_file_line_match(self, match, line):
        """ Appends file, line and lineno to self.dic['__pl'] so that it can be later processed
            by loader.loader.
            
            Raise loader.exceptions.SyntaxErrorPL if no group 'file' was found."""

        if not match.group('file'):
            raise SyntaxErrorPL(self.path_parsed_file, line, self.lineno)

        directory, path = get_location(self.directory,
                                       match.group('file'),
                                       current=self.path_parsed_file)

        self.dic['__pl'].append({
            'path': path.replace(directory.name + '/', ''),
            'line': line,
            'lineno': self.lineno,
            'directory_name': directory.name
        })
Esempio n. 9
0
    def url_line_match(self, match, line):
        """ Map value to a download url of a resource.

            Raise from loader.exceptions:
                - SyntaxErrorPL if no group 'key' or 'file' was found
                - DirectoryNotFound if trying to load from a nonexistent directory
                - FileNotFound if the given file do not exists."""
        
        key = match.group('key')
        
        try:
            directory, path = get_location(self.directory, match.group('file'),
                                           current=dirname(self.path), parser=self)
            url = to_download_url(os.path.join(directory, path))
            self.dic_add_key(key, url)
        except FileNotFoundError as e:
            raise FileNotFound(self.path_parsed_file, line, match.group('file'), self.lineno,
                               str(e))
        except SyntaxError as e:
            raise SyntaxErrorPL(self.path_parsed_file, line, self.lineno, str(e))
Esempio n. 10
0
def resolve_path(request):  # TODO ADD TEST
    path = request.GET.get('path')
    if not path:
        return HttpResponseBadRequest(missing_parameter('path'))
    target = request.GET.get('target')
    if not target:
        return HttpResponseBadRequest(missing_parameter('target'))
    
    try:
        path_components = path.split('/')
        directory = Directory.objects.get(name=path_components[0])
        current = path_components[1] if len(path_components) == 2 else \
            os.path.join(*path_components[1:-1])
        directory, path = get_location(directory, target, current=current)
        return HttpResponse(os.path.join(directory, path))
    
    except Exception as e:
        msg = "Impossible to resolve the path '" + request.GET.get(
            'target') + "' : " + htmlprint.code(str(type(e)) + ' - ' + str(e))
        if settings.DEBUG:
            messages.error(request, "DEBUG set to True: " + htmlprint.html_exc())
        return HttpResponseNotFound(msg)
Esempio n. 11
0
 def extends_line_match(self, match, line):
     """ Appends file, line and lineno to self.dic['__extends'] so that it can be later processed
         by loader.parser.
         
         Raise from loader.exceptions
             - SyntaxErrorPL if no group 'file' was found.
             - DirectoryNotFound if the directory indicated by the pl couldn't be found"""
     
     if not match.group('file'):
         raise SyntaxErrorPL(self.path_parsed_file, line, self.lineno)
     
     try:
         directory, path = get_location(self.directory, match.group('file'), current=self.path_parsed_file)
     except ObjectDoesNotExist:
         raise DirectoryNotFound(self.path_parsed_file, line, match.group('file'), self.lineno)
         
     self.dic['__extends'].append({
         'path': path.replace(directory.name+'/', ''),
         'line': line,
         'lineno': self.lineno,
         'directory_name': directory.name
     })
Esempio n. 12
0
 def sandbox_file_line_match(self, match, line):
     """ Map content of file to self.dic['__file'][name].
         
         Raise from loader.exceptions:
             - SyntaxErrorPL if no group 'file' was found
             - DirectoryNotFound if trying to load from a nonexistent directory
             - FileNotFound if the given file do not exists."""
     
     if not match.group('file'):
         raise SyntaxErrorPL(self.path_parsed_file, line, self.lineno)
     
     try:
         directory, path = get_location(self.directory, match.group('file'), current=self.path_parsed_file)
         path = join(directory.root, path)
         name = basename(path) if not match.group('alias') else match.group('alias')
         with open(path, 'r') as f:
             self.dic['__file'][name] = f.read()
     except ObjectDoesNotExist:
         raise DirectoryNotFound(self.path_parsed_file, line, match.group('file'), self.lineno)
     except FileNotFoundError:
         raise FileNotFound(self.path_parsed_file, line, path, lineno=self.lineno)
     except ValueError:
         raise FileNotFound(self.path_parsed_file, line, match.group('file'), lineno=self.lineno, message="Path from another directory must be absolute")
Esempio n. 13
0
    def pl_file_line_match(self, match, line):
        """ Appends file, line and lineno to self.dic['__pl'] so that it can be later processed
            by loader.loader.

            Raise loader.exceptions.SyntaxErrorPL if no group 'file' was found."""

        try:
            directory, path = get_location(self.directory,
                                           match.group('file'),
                                           current=dirname(self.path),
                                           parser=self)
        except SyntaxError as e:
            raise SyntaxErrorPL(self.path_parsed_file, line, self.lineno,
                                str(e))
        except FileNotFoundError as e:
            raise FileNotFound(self.path_parsed_file, line,
                               match.group('file'), self.lineno, str(e))

        self.dic['__pl'].append({
            'path': path,
            'line': line,
            'lineno': self.lineno,
            'directory_name': directory
        })
Esempio n. 14
0
    def test_get_location(self):
        current_repo = 'repo1/'
        current_dir = 'dir1/'

        absolute = '~/repo1/file1.pl'
        rel_repo = '/file2.pl'
        relative = '../dir1/file3.pl'
        ref_lib = 'lib:/dir2/file4.pl'

        # Absolute outside of repo
        self.assertEqual((self.dir.name, 'repo1/file1.pl'),
                         get_location(self.dir, absolute, current_dir))
        # Absolute in a repo
        self.assertEqual((self.dir.name, 'repo1/file1.pl'),
                         get_location(self.dir, absolute, current_repo))

        # Relative to repo
        self.assertEqual((self.dir.name, 'repo1/file2.pl'),
                         get_location(self.dir, rel_repo, current_repo))

        # Relative outside of repo
        self.assertEqual((self.dir.name, 'dir1/file3.pl'),
                         get_location(self.dir, relative, current_dir))
        # Relative in a repo
        self.assertEqual((self.dir.name, 'dir1/file3.pl'),
                         get_location(self.dir, relative, current_repo))

        # Reference to a lib outside of repo
        self.assertEqual((self.lib.name, 'dir2/file4.pl'),
                         get_location(self.dir, ref_lib, current_dir))
        # Reference to a lib in a repo
        self.assertEqual((self.lib.name, 'dir2/file4.pl'),
                         get_location(self.dir, ref_lib, current_repo))

        with self.assertRaises(SyntaxError):
            get_location(self.dir, "lib:without/stating/slash", "")

        with self.assertRaises(SyntaxError):
            get_location(self.dir, "1234:lib/digit", "")

        with self.assertRaises(SyntaxError):
            get_location(self.dir, "/lib/digit", "")

        with self.assertRaises(FileNotFoundError):
            get_location(self.dir, "unknown:/file.pl", "")

        with self.assertRaises(FileNotFoundError):
            get_location(self.dir, "lib:/unknown.pl", "")

        with self.assertRaises(FileNotFoundError):
            get_location(self.dir, "~/unknown.pl", "")

        with self.assertRaises(FileNotFoundError):
            get_location(self.dir, "/unknown.pl", "repo1/")

        with self.assertRaises(FileNotFoundError):
            get_location(self.dir, "unknown.pl", "dir1/")