Ejemplo n.º 1
0
 def temp_url_for(self, **kwargs):
     filename = kwargs.get('filename', self.filename)
     seconds = kwargs.get('seconds', None)
     if type(seconds) is float:
         seconds = int(seconds)
     if type(seconds) is not int:
         seconds = 30
     if cloud is not None:
         keyname = str(self.section) + '/' + str(self.file_number) + '/' + str(filename)
         key = cloud.get_key(keyname)
         if key.does_exist:
             if 'display_filename' in kwargs:
                 return key.generate_url(seconds, display_filename=kwargs['display_filename'])
             else:
                 return key.generate_url(seconds)
         else:
             sys.stderr.write("key " + str(keyname) + " did not exist\n")
             return('about:blank')
     r = docassemble.base.functions.server.server_redis
     while True:
         code = random_alphanumeric(32)
         keyname = 'da:tempfile:' + code
         if r.setnx(keyname, str(self.section) + '^' + str(self.file_number)):
             r.expire(keyname, seconds)
             break
     return docassemble.base.functions.get_url_root() + '/tempfile/' + code + '/' + kwargs.get('display_filename', self.filename)
Ejemplo n.º 2
0
def reference_exists(file_reference):
    if cloud:
        parts = file_reference.split(":")
        if len(parts) == 2:
            m = re.search(r'^docassemble.playground([0-9]+)$', parts[0])
            if m:
                user_id = m.group(1)
                if re.search(r'^data/sources/', parts[1]):
                    section = 'playgroundsources'
                    filename = re.sub(r'^data/sources/', '', parts[1])
                else:
                    section = 'playgroundstatic'
                    filename = re.sub(r'^data/static/', '', parts[1])
                filename = re.sub(r'[^A-Za-z0-9\-\_\. ]', '', filename)
                key = str(section) + '/' + str(user_id) + '/' + filename
                cloud_key = cloud.get_key(key)
                if cloud_key.does_exist:
                    return True
                return False
    the_path = docassemble.base.functions.static_filename_path(file_reference)
    if the_path is None or not os.path.isfile(the_path):
        #logmessage("Returning false")
        return False
    #logmessage("Returning true because path is " + str(the_path))
    return True
Ejemplo n.º 3
0
def main():
    from docassemble.base.config import daconfig
    container_role = ':' + os.environ.get('CONTAINERROLE', '') + ':'
    if ':all:' in container_role or ':cron:' in container_role:
        import docassemble.webapp.create_tables
        docassemble.webapp.create_tables.main()
        if ':cron:' in container_role:
            import redis
            (redis_host, redis_port, redis_password, redis_offset,
             redis_cli) = docassemble.base.config.parse_redis_uri()
            r = redis.StrictRedis(host=redis_host,
                                  port=redis_port,
                                  db=redis_offset,
                                  password=redis_password)
            r.delete('da:cron_restart')

    webapp_path = daconfig.get(
        'webapp', '/usr/share/docassemble/webapp/docassemble.wsgi')
    import docassemble.webapp.cloud
    cloud = docassemble.webapp.cloud.get_cloud()
    if cloud is not None:
        key = cloud.get_key('config.yml')
        if key.does_exist:
            key.get_contents_to_filename(daconfig['config file'])
            sys.stderr.write("Wrote config file based on copy on cloud\n")
    wsgi_file = webapp_path
    if os.path.isfile(wsgi_file):
        with open(wsgi_file, 'a'):
            os.utime(wsgi_file, None)
            sys.stderr.write("Restarted.\n")
    sys.exit(0)
Ejemplo n.º 4
0
def url_if_exists(file_reference, **kwargs):
    parts = file_reference.split(":")
    if len(parts) == 2:
        if cloud:
            m = re.search(r'^docassemble.playground([0-9]+)$', parts[0])
            if m:
                user_id = m.group(1)
                if re.search(r'^data/sources/', parts[1]):
                    section = 'playgroundsources'
                    filename = re.sub(r'^data/sources/', '', parts[1])
                else:
                    section = 'playgroundstatic'
                    filename = re.sub(r'^data/static/', '', parts[1])
                filename = re.sub(r'[^A-Za-z0-9\-\_\. ]', '', filename)
                key = str(section) + '/' + str(user_id) + '/' + filename
                cloud_key = cloud.get_key(key)
                if cloud_key.does_exist:
                    if not kwargs.get('inline', False):
                        return cloud_key.generate_url(3600, display_filename=filename)
                    else:
                        return cloud_key.generate_url(3600)
                return None
        the_path = docassemble.base.functions.static_filename_path(file_reference)
        if the_path is None or not os.path.isfile(the_path):
            return None
        return docassemble.base.config.daconfig.get('root', '/') + 'packagestatic/' + parts[0] + '/' + re.sub(r'^data/static/', '', parts[1])
    return None
Ejemplo n.º 5
0
 def finalize(self):
     if cloud is None:
         return
     if not self.fixed:
         raise DAError("SavedFile: finalize called before fix")
     existing_files = list()
     for filename in os.listdir(self.directory):
         existing_files.append(filename)
         fullpath = os.path.join(self.directory, filename)
         #logmessage("Found " + fullpath)
         if os.path.isfile(fullpath):
             save = True
             if filename in self.keydict:
                 key = self.keydict[filename]
                 if self.modtimes[filename] == os.path.getmtime(fullpath):
                     save = False
             else:
                 key = cloud.get_key(
                     str(self.section) + '/' + str(self.file_number) + '/' +
                     str(filename))
                 if self.extension is not None and filename == self.filename:
                     extension, mimetype = get_ext_and_mimetype(
                         filename + '.' + self.extension)
                 else:
                     extension, mimetype = get_ext_and_mimetype(filename)
                 key.content_type = mimetype
             if save:
                 key.set_contents_from_filename(fullpath)
     for filename, key in self.keydict.iteritems():
         if filename not in existing_files:
             #logmessage("Deleting filename " + str(filename) + " from cloud")
             key.delete()
     return
Ejemplo n.º 6
0
def url_if_exists(file_reference, **kwargs):
    parts = file_reference.split(":")
    if len(parts) == 2:
        if cloud:
            m = re.search(r'^docassemble.playground([0-9]+)$', parts[0])
            if m:
                user_id = m.group(1)
                if re.search(r'^data/sources/', parts[1]):
                    section = 'playgroundsources'
                    filename = re.sub(r'^data/sources/', '', parts[1])
                    filename = re.sub(r'[^A-Za-z0-9\-\_\. ]', '', filename)
                    key = str(section) + '/' + str(user_id) + '/' + filename
                    cloud_key = cloud.get_key(key)
                    if cloud_key.does_exist:
                        if not kwargs.get('inline', False):
                            return cloud_key.generate_url(
                                3600, display_filename=filename)
                        else:
                            return cloud_key.generate_url(3600)
                    return None
                section = 'playgroundstatic'
                filename = re.sub(r'^data/static/', '', parts[1])
                version_parameter = get_version_parameter(parts[0])
                return docassemble.base.config.daconfig.get(
                    'root', '/') + 'packagestatic/' + parts[0] + '/' + re.sub(
                        r'^data/static/', '', parts[1]) + version_parameter
        the_path = docassemble.base.functions.static_filename_path(
            file_reference)
        if the_path is None or not os.path.isfile(the_path):
            return None
        version_parameter = get_version_parameter(parts[0])
        return docassemble.base.config.daconfig.get(
            'root', '/') + 'packagestatic/' + parts[0] + '/' + re.sub(
                r'^data/static/', '', parts[1]) + version_parameter
    return None
Ejemplo n.º 7
0
 def temp_url_for(self, **kwargs):
     filename = kwargs.get('filename', self.filename)
     seconds = kwargs.get('seconds', None)
     if type(seconds) is float:
         seconds = int(seconds)
     if type(seconds) is not int:
         seconds = 30
     if cloud is not None:
         keyname = str(self.section) + '/' + str(self.file_number) + '/' + str(filename)
         key = cloud.get_key(keyname)
         if key.does_exist:
             if 'display_filename' in kwargs:
                 return key.generate_url(seconds, display_filename=kwargs['display_filename'])
             else:
                 return key.generate_url(seconds)
         else:
             sys.stderr.write("key " + str(keyname) + " did not exist\n")
             return('about:blank')
     r = docassemble.base.functions.server.server_redis
     while True:
         code = random_alphanumeric(32)
         keyname = 'da:tempfile:' + code
         if r.setnx(keyname, str(self.section) + '^' + str(self.file_number)):
             r.expire(keyname, seconds)
             break
     return docassemble.base.functions.get_url_root() + '/tempfile/' + code + '/' + kwargs.get('display_filename', self.filename)
Ejemplo n.º 8
0
def url_if_exists(file_reference):
    parts = file_reference.split(":")
    if len(parts) == 2:
        if cloud:
            m = re.search(r'^docassemble.playground([0-9]+)$', parts[0])
            if m:
                user_id = m.group(1)
                if re.search(r'^data/sources/', parts[1]):
                    section = 'playgroundsources'
                    filename = re.sub(r'^data/sources/', '', parts[1])
                else:
                    section = 'playgroundstatic'
                    filename = re.sub(r'^data/static/', '', parts[1])
                filename = re.sub(r'[^A-Za-z0-9\-\_\. ]', '', filename)
                key = str(section) + '/' + str(user_id) + '/' + filename
                cloud_key = cloud.get_key(key)
                if cloud_key.does_exist:
                    return cloud_key.generate_url(3600,
                                                  display_filename=filename)
                return None
        the_path = docassemble.base.functions.static_filename_path(
            file_reference)
        if the_path is None or not os.path.isfile(the_path):
            return None
        return url_for('package_static',
                       package=parts[0],
                       filename=re.sub(r'^data/static/', '', parts[1]))
    return None
Ejemplo n.º 9
0
 def temp_url_for(self, **kwargs):
     if kwargs.get('_attachment', False):
         suffix = 'download'
     else:
         suffix = ''
     filename = kwargs.get('filename', self.filename)
     seconds = kwargs.get('seconds', None)
     if isinstance(seconds, float):
         seconds = int(seconds)
     if not isinstance(seconds, int):
         seconds = 30
     if cloud is not None and daconfig.get('use cloud urls', False):
         keyname = str(self.section) + '/' + str(self.file_number) + '/' + path_to_key(filename)
         key = cloud.get_key(keyname)
         inline = not bool(kwargs.get('_attachment', False))
         if key.does_exist:
             return key.generate_url(seconds, display_filename=kwargs.get('display_filename', None), inline=inline, content_type=kwargs.get('content_type', None))
         sys.stderr.write("key " + str(keyname) + " did not exist\n")
         return 'about:blank'
     r = docassemble.base.functions.server.server_redis
     while True:
         code = random_alphanumeric(32)
         keyname = 'da:tempfile:' + code
         if r.setnx(keyname, str(self.section) + '^' + str(self.file_number)):
             r.expire(keyname, seconds)
             break
     use_external = kwargs.get('_external', bool('jsembed' in docassemble.base.functions.this_thread.misc))
     url = url_for('rootindex', _external=use_external).rstrip('/')
     url += '/tempfile' + suffix + '/' + code + '/' + path_to_key(kwargs.get('display_filename', filename))
     return url
Ejemplo n.º 10
0
def reference_exists(file_reference):
    if cloud:
        parts = file_reference.split(":")
        if len(parts) == 2:
            m = re.search(r'^docassemble.playground([0-9]+)$', parts[0])
            if m:
                user_id = m.group(1)
                if re.search(r'^data/sources/', parts[1]):
                    section = 'playgroundsources'
                    filename = re.sub(r'^data/sources/', '', parts[1])
                else:
                    section = 'playgroundstatic'
                    filename = re.sub(r'^data/static/', '', parts[1])
                filename = re.sub(r'[^A-Za-z0-9\-\_\. ]', '', filename)
                key = str(section) + '/' + str(user_id) + '/' + filename
                cloud_key = cloud.get_key(key)
                if cloud_key.does_exist:
                    return True
                return False
    the_path = docassemble.base.functions.static_filename_path(file_reference)
    if the_path is None or not os.path.isfile(the_path):
        #logmessage("Returning false")
        return False
    #logmessage("Returning true because path is " + str(the_path))
    return True
Ejemplo n.º 11
0
 def temp_url_for(self, **kwargs):
     filename = kwargs.get('filename', self.filename)
     seconds = kwargs.get('seconds', None)
     if type(seconds) is float:
         seconds = int(seconds)
     if type(seconds) is not int:
         seconds = 30
     if cloud is not None and daconfig.get('use cloud urls', False):
         keyname = str(self.section) + '/' + str(self.file_number) + '/' + path_to_key(filename)
         key = cloud.get_key(keyname)
         if key.does_exist:
             return key.generate_url(seconds, display_filename=kwargs.get('display_filename', None), inline=kwargs.get('inline', None), content_type=kwargs.get('content_type', None))
         else:
             sys.stderr.write("key " + str(keyname) + " did not exist\n")
             return('about:blank')
     r = docassemble.base.functions.server.server_redis
     while True:
         code = random_alphanumeric(32)
         keyname = 'da:tempfile:' + code
         if r.setnx(keyname, str(self.section) + '^' + str(self.file_number)):
             r.expire(keyname, seconds)
             break
     use_external = kwargs.get('_external', True if 'jsembed' in docassemble.base.functions.this_thread.misc else False)
     from flask import url_for
     url = url_for('rootindex', _external=use_external).rstrip('/')
     url += '/tempfile/' + code + '/' + path_to_key(kwargs.get('display_filename', filename))
     return(url)
Ejemplo n.º 12
0
 def url_for(self, **kwargs):
     if 'ext' in kwargs and kwargs['ext'] is not None:
         extn = kwargs['ext']
         extn = re.sub(r'^\.', '', extn)
     else:
         extn = None
     filename = kwargs.get('filename', self.filename)
     if cloud is not None:
         keyname = str(self.section) + '/' + str(
             self.file_number) + '/' + str(filename)
         page = kwargs.get('page', None)
         if page:
             size = kwargs.get('size', 'page')
             page = re.sub(r'[^0-9]', '', page)
             if size == 'screen':
                 keyname += 'screen-' + str(page) + '.png'
             else:
                 keyname += 'page-' + str(page) + '.png'
         elif extn:
             keyname += '.' + extn
         key = cloud.get_key(keyname)
         if key.exists():
             if 'display_filename' in kwargs:
                 return (key.generate_url(
                     3600, display_filename=kwargs['display_filename']))
             else:
                 return (key.generate_url(3600))
         else:
             return ('about:blank')
     else:
         if extn is None:
             extn = ''
         else:
             extn = '.' + extn
         root = daconfig.get('root', '/')
         fileroot = daconfig.get('fileserver', root)
         if 'display_filename' in kwargs:
             filename = kwargs['display_filename']
         if self.section == 'files':
             if 'page' in kwargs and kwargs['page']:
                 page = re.sub(r'[^0-9]', '', str(kwargs['page']))
                 size = kwargs.get('size', 'page')
                 url = fileroot + 'uploadedpage'
                 if size == 'screen':
                     url += 'screen'
                 url += '/' + str(self.file_number) + '/' + str(page)
             else:
                 if re.search(r'\.', str(filename)):
                     url = fileroot + 'uploadedfile/' + str(
                         self.file_number) + '/' + str(filename)
                 elif extn != '':
                     url = fileroot + 'uploadedfile/' + str(
                         self.file_number) + '/' + str(filename) + extn
                 else:
                     url = fileroot + 'uploadedfile/' + str(
                         self.file_number)
         else:
             url = 'about:blank'
         return (url)
Ejemplo n.º 13
0
def check_for_config():
    from docassemble.base.config import daconfig
    import docassemble.webapp.cloud
    cloud = docassemble.webapp.cloud.get_cloud()
    if cloud is not None:
        key = cloud.get_key('config.yml')
        if key.does_exist:
            key.get_contents_to_filename(daconfig['config file'])
Ejemplo n.º 14
0
def check_for_config():
    from docassemble.base.config import daconfig
    import docassemble.webapp.cloud
    cloud = docassemble.webapp.cloud.get_cloud()
    if cloud is not None:
        key = cloud.get_key('config.yml')
        if key.does_exist:
            key.get_contents_to_filename(daconfig['config file'])
Ejemplo n.º 15
0
 def url_for(self, **kwargs):
     if 'ext' in kwargs and kwargs['ext'] is not None:
         extn = kwargs['ext']
         extn = re.sub(r'^\.', '', extn)
     else:
         extn = None
     filename = kwargs.get('filename', self.filename)
     if cloud is not None and not (self.section == 'files' and 'page' in kwargs and kwargs['page']) and daconfig.get('use cloud urls', False):
         keyname = str(self.section) + '/' + str(self.file_number) + '/' + path_to_key(filename)
         page = kwargs.get('page', None)
         if page:
             size = kwargs.get('size', 'page')
             page = re.sub(r'[^0-9]', '', str(page))
             if size == 'screen':
                 keyname += 'screen-' + str(page) + '.png'
             else:
                 keyname += 'page-' + str(page) + '.png'
         elif extn:
             keyname += '.' + extn
         key = cloud.get_key(keyname)
         inline = not bool(kwargs.get('_attachment', False))
         if key.does_exist:
             return key.generate_url(3600, display_filename=kwargs.get('display_filename', None), inline=inline, content_type=kwargs.get('content_type', None))
         # why not serve right from uploadedpage in this case?
         sys.stderr.write("key " + str(keyname) + " did not exist\n")
         return 'about:blank'
     if kwargs.get('_attachment', False):
         suffix = 'download'
     else:
         suffix = ''
     use_external = kwargs.get('_external', bool('jsembed' in docassemble.base.functions.this_thread.misc))
     base_url = url_for('rootindex', _external=use_external).rstrip('/')
     if extn is None:
         extn = ''
     else:
         extn = '.' + extn
     filename = kwargs.get('display_filename', filename)
     if self.section == 'files':
         if 'page' in kwargs and kwargs['page']:
             page = re.sub(r'[^0-9]', '', str(kwargs['page']))
             size = kwargs.get('size', 'page')
             url = base_url + '/uploadedpage'
             if size == 'screen':
                 url += 'screen'
             url += suffix
             url += '/' + str(self.file_number) + '/' + str(page)
         else:
             if re.search(r'\.', str(filename)):
                 url = base_url + '/uploadedfile' + suffix + '/' + str(self.file_number) + '/' + path_to_key(filename)
             elif extn != '':
                 url = base_url + '/uploadedfile' + suffix + '/' + str(self.file_number) + '/' + path_to_key(filename) + extn
             else:
                 url = base_url + '/uploadedfile' + suffix + '/' + str(self.file_number)
     else:
         sys.stderr.write("section " + self.section + " was wrong\n")
         url = 'about:blank'
     return url
Ejemplo n.º 16
0
 def url_for(self, **kwargs):
     if 'ext' in kwargs and kwargs['ext'] is not None:
         extn = kwargs['ext']
         extn = re.sub(r'^\.', '', extn)
     else:
         extn = None
     filename = kwargs.get('filename', self.filename)
     if cloud is not None and not (self.section == 'files' and 'page' in kwargs and kwargs['page']):
         keyname = str(self.section) + '/' + str(self.file_number) + '/' + path_to_key(filename)
         page = kwargs.get('page', None)
         if page:
             size = kwargs.get('size', 'page')
             page = re.sub(r'[^0-9]', '', str(page))
             if size == 'screen':
                 keyname += 'screen-' + str(page) + '.png'
             else:
                 keyname += 'page-' + str(page) + '.png'
         elif extn:
             keyname += '.' + extn
         key = cloud.get_key(keyname)
         if key.does_exist:
             return key.generate_url(3600, display_filename=kwargs.get('display_filename', None), inline=kwargs.get('inline', None), content_type=kwargs.get('content_type', None))
         else:
             #logmessage("Key " + str(keyname) + " did not exist")
             #why not serve right from uploadedpage in this case?
             sys.stderr.write("key " + str(keyname) + " did not exist\n")
             return('about:blank')
     else:
         use_external = kwargs.get('_external', True if 'jsembed' in docassemble.base.functions.this_thread.misc else False)
         from flask import url_for
         base_url = url_for('rootindex', _external=use_external).rstrip('/')
         if extn is None:
             extn = ''
         else:
             extn = '.' + extn
         if 'display_filename' in kwargs:
             filename = kwargs['display_filename']
         if self.section == 'files':
             if 'page' in kwargs and kwargs['page']:
                 page = re.sub(r'[^0-9]', '', str(kwargs['page']))
                 size = kwargs.get('size', 'page')
                 url = base_url + '/uploadedpage'
                 if size == 'screen':
                     url += 'screen'
                 url += '/' + str(self.file_number) + '/' + str(page)
             else:
                 if re.search(r'\.', str(filename)):
                     url = base_url + '/uploadedfile/' + str(self.file_number) + '/' + path_to_key(filename)
                 elif extn != '':
                     url = base_url + '/uploadedfile/' + str(self.file_number) + '/' + path_to_key(filename) + extn
                 else:
                     url = base_url + '/uploadedfile/' + str(self.file_number)
         else:
             sys.stderr.write("section " + section + " was wrong\n")
             url = 'about:blank'
         return(url)
Ejemplo n.º 17
0
def main():
    roles = os.environ.get('CONTAINERROLE', None)
    if roles is None:
        return
    cloud = docassemble.webapp.cloud.get_cloud()
    if cloud is not None:
        roles = re.sub(r'^:+|:+$', r'', roles)
        role_list = roles.split(":")
        if 'all' in role_list:
            role_list = ['sql', 'redis', 'rabbitmq']
        for role in role_list:
            if role in ['sql', 'log', 'redis', 'rabbitmq']:
                key = cloud.get_key('hostname-' + role)
                if key.does_exist:
                    key.delete()
                if role == 'rabbitmq':
                    key_two = cloud.get_key('ip-rabbitmq')
                    if key_two.does_exist:
                        key_two.delete()
Ejemplo n.º 18
0
def main():
    roles = os.environ.get('CONTAINERROLE', None)
    if roles is None:
        return
    cloud = docassemble.webapp.cloud.get_cloud()
    if cloud is not None:
        roles = re.sub(r'^:+|:+$', r'', roles)
        role_list = roles.split(":")
        if 'all' in role_list:
            role_list = ['sql', 'redis', 'rabbitmq']
        for role in role_list:
            if role in ['sql', 'log', 'redis', 'rabbitmq']:
                key = cloud.get_key('hostname-' + role)
                if role == 'rabbitmq':
                    key.set_contents_from_string(socket.gethostname())
                    key_two = cloud.get_key('ip-rabbitmq')
                    key_two.set_contents_from_string(
                        socket.gethostbyname_ex(hostname)[2][0])
                else:
                    key.set_contents_from_string(hostname)
Ejemplo n.º 19
0
def main():
    from docassemble.base.config import hostname
    roles = os.environ.get('CONTAINERROLE', None)
    if roles is None:
        return
    import docassemble.webapp.cloud
    cloud = docassemble.webapp.cloud.get_cloud()
    if cloud is not None:
        roles = re.sub(r'^:+|:+$', r'', roles)
        role_list = roles.split(":")
        if 'all' in role_list:
            role_list = ['sql', 'redis', 'rabbitmq']
        for role in role_list:
            if role in ['sql', 'log', 'redis', 'rabbitmq']:
                key = cloud.get_key('hostname-' + role)
                if key.does_exist:
                    key.delete()
                if role == 'rabbitmq':
                    key_two = cloud.get_key('ip-rabbitmq')
                    if key_two.does_exist:
                        key_two.delete()
Ejemplo n.º 20
0
def main():
    import docassemble.webapp.cloud
    cloud = docassemble.webapp.cloud.get_cloud()
    if cloud is not None:
        key = cloud.get_key('config.yml')
        if key.exists():
            key.get_contents_to_filename(config_file)
            sys.stderr.write("Wrote config file based on copy on cloud\n")
    wsgi_file = WEBAPP_PATH
    if os.path.isfile(wsgi_file):
        with open(wsgi_file, 'a'):
            os.utime(wsgi_file, None)
    sys.exit(0)
Ejemplo n.º 21
0
 def finalize(self):
     #sys.stderr.write("finalize: starting " + str(self.section) + '/' + str(self.file_number) + "\n")
     if cloud is None:
         return
     if not self.fixed:
         raise DAError("SavedFile: finalize called before fix")
     for filename in listfiles(self.directory):
         fullpath = os.path.join(self.directory, filename)
         #logmessage("Found " + fullpath)
         if os.path.isfile(fullpath):
             save = True
             if filename in self.keydict:
                 key = self.keydict[filename]
                 if self.modtimes[filename] == os.path.getmtime(fullpath):
                     save = False
             else:
                 key = cloud.get_key(
                     str(self.section) + '/' + str(self.file_number) + '/' +
                     path_to_key(filename))
             if save:
                 if self.extension is not None and filename == self.filename:
                     extension, mimetype = get_ext_and_mimetype(
                         filename + '.' + self.extension)
                 else:
                     extension, mimetype = get_ext_and_mimetype(filename)
                 key.content_type = mimetype
                 #sys.stderr.write("finalize: saving " + str(self.section) + '/' + str(self.file_number) + '/' + str(filename) + "\n")
                 if not os.path.isfile(fullpath):
                     continue
                 try:
                     key.set_contents_from_filename(fullpath)
                     self.modtimes[filename] = key.get_epoch_modtime()
                 except FileNotFoundError:
                     sys.stderr.write("finalize: error while saving " +
                                      str(self.section) + '/' +
                                      str(self.file_number) + '/' +
                                      str(filename) + "; path " +
                                      str(fullpath) + " disappeared\n")
     for filename, key in self.keydict.items():
         if not os.path.isfile(os.path.join(self.directory, filename)):
             sys.stderr.write("finalize: deleting " + str(self.section) +
                              '/' + str(self.file_number) + '/' +
                              path_to_key(filename) + "\n")
             try:
                 key.delete()
             except:
                 pass
     #sys.stderr.write("finalize: ending " + str(self.section) + '/' + str(self.file_number) + "\n")
     return
Ejemplo n.º 22
0
def url_if_exists(file_reference, **kwargs):
    attach_parameter = '&attachment=1' if kwargs.get('_attachment',
                                                     False) else ''
    parts = file_reference.split(":")
    from flask import url_for
    base_url = url_for('rootindex', _external=kwargs.get('_external',
                                                         False)).rstrip('/')
    if len(parts) == 2:
        if cloud and docassemble.base.config.daconfig.get(
                'use cloud urls', False):
            m = re.search(r'^docassemble.playground([0-9]+)(.*)$', parts[0])
            if m:
                user_id = m.group(1)
                project = m.group(2)
                if re.search(r'^data/sources/', parts[1]):
                    section = 'playgroundsources'
                    filename = re.sub(r'^data/sources/', '', parts[1])
                    filename = re.sub(r'[^A-Za-z0-9\-\_\. ]', '', filename)
                    if project == '':
                        key = str(section) + '/' + str(
                            user_id) + '/' + filename
                    else:
                        key = str(section) + '/' + str(
                            user_id) + '/' + project + '/' + filename
                    cloud_key = cloud.get_key(key)
                    if cloud_key.does_exist:
                        if kwargs.get('_attachment', False):
                            return cloud_key.generate_url(
                                3600, display_filename=filename)
                        else:
                            return cloud_key.generate_url(3600)
                    return None
                section = 'playgroundstatic'
                filename = re.sub(r'^data/static/', '', parts[1])
                version_parameter = get_version_parameter(parts[0])
                return base_url + '/packagestatic/' + parts[0] + '/' + re.sub(
                    r'^data/static/', '',
                    parts[1]) + version_parameter + attach_parameter
        the_path = docassemble.base.functions.static_filename_path(
            file_reference)
        if the_path is None or not os.path.isfile(the_path):
            return None
        version_parameter = get_version_parameter(parts[0])
        return base_url + '/packagestatic/' + parts[0] + '/' + re.sub(
            r'^data/static/', '',
            parts[1]) + version_parameter + attach_parameter
    return None
Ejemplo n.º 23
0
def main():
    from docassemble.base.config import daconfig
    container_role = os.environ.get('CONTAINERROLE', None)
    if container_role and re.search(r':(all|cron):', container_role):
        import docassemble.webapp.fix_postgresql_tables
        docassemble.webapp.fix_postgresql_tables.main()
        import docassemble.webapp.create_tables
        docassemble.webapp.create_tables.main()

    webapp_path = daconfig.get('webapp', '/usr/share/docassemble/webapp/docassemble.wsgi')
    import docassemble.webapp.cloud
    cloud = docassemble.webapp.cloud.get_cloud()
    if cloud is not None:
        key = cloud.get_key('config.yml')
        if key.does_exist:
            key.get_contents_to_filename(daconfig['config file'])
            sys.stderr.write("Wrote config file based on copy on cloud\n")
    wsgi_file = webapp_path
    if os.path.isfile(wsgi_file):
        with open(wsgi_file, 'a'):
            os.utime(wsgi_file, None)
            sys.stderr.write("Restarted.\n")
    sys.exit(0)
Ejemplo n.º 24
0
def main():
    from docassemble.base.config import daconfig
    container_role = os.environ.get('CONTAINERROLE', None)
    if container_role and re.search(r':(all|cron):', container_role):
        import docassemble.webapp.fix_postgresql_tables
        docassemble.webapp.fix_postgresql_tables.main()
        import docassemble.webapp.create_tables
        docassemble.webapp.create_tables.main()

    webapp_path = daconfig.get(
        'webapp', '/usr/share/docassemble/webapp/docassemble.wsgi')
    import docassemble.webapp.cloud
    cloud = docassemble.webapp.cloud.get_cloud()
    if cloud is not None:
        key = cloud.get_key('config.yml')
        if key.does_exist:
            key.get_contents_to_filename(daconfig['config file'])
            sys.stderr.write("Wrote config file based on copy on cloud\n")
    wsgi_file = webapp_path
    if os.path.isfile(wsgi_file):
        with open(wsgi_file, 'a'):
            os.utime(wsgi_file, None)
    sys.exit(0)
Ejemplo n.º 25
0
 def finalize(self):
     #sys.stderr.write("finalize: starting " + str(self.section) + '/' + str(self.file_number) + "\n")
     if cloud is None:
         return
     if not self.fixed:
         raise DAError("SavedFile: finalize called before fix")
     existing_files = list()
     for filename in os.listdir(self.directory):
         existing_files.append(filename)
         fullpath = os.path.join(self.directory, filename)
         #logmessage("Found " + fullpath)
         if os.path.isfile(fullpath):
             save = True
             if filename in self.keydict:
                 key = self.keydict[filename]
                 if self.modtimes[filename] == os.path.getmtime(fullpath):
                     save = False
             else:
                 key = cloud.get_key(str(self.section) + '/' + str(self.file_number) + '/' + str(filename))
             if save:
                 if self.extension is not None and filename == self.filename:
                     extension, mimetype = get_ext_and_mimetype(filename + '.' + self.extension)
                 else:
                     extension, mimetype = get_ext_and_mimetype(filename)
                 key.content_type = mimetype
                 #sys.stderr.write("finalize: saving " + str(self.section) + '/' + str(self.file_number) + '/' + str(filename) + "\n")
                 key.set_contents_from_filename(fullpath)
                 self.modtimes[filename] = key.get_epoch_modtime()
     for filename, key in self.keydict.iteritems():
         if filename not in existing_files:
             #sys.stderr.write("finalize: deleting " + str(self.section) + '/' + str(self.file_number) + '/' + str(filename) + "\n")
             try:
                 key.delete()
             except:
                 pass
     #sys.stderr.write("finalize: ending " + str(self.section) + '/' + str(self.file_number) + "\n")
     return
Ejemplo n.º 26
0
 def url_for(self, **kwargs):
     if 'ext' in kwargs and kwargs['ext'] is not None:
         extn = kwargs['ext']
         extn = re.sub(r'^\.', '', extn)
     else:
         extn = None
     filename = kwargs.get('filename', self.filename)
     use_external = kwargs.get('_external', False)
     if cloud is not None and not (self.section == 'files' and 'page' in kwargs and kwargs['page']):
         keyname = str(self.section) + '/' + str(self.file_number) + '/' + str(filename)
         page = kwargs.get('page', None)
         if page:
             size = kwargs.get('size', 'page')
             page = re.sub(r'[^0-9]', '', str(page))
             if size == 'screen':
                 keyname += 'screen-' + str(page) + '.png'
             else:
                 keyname += 'page-' + str(page) + '.png'
         elif extn:
             keyname += '.' + extn
         key = cloud.get_key(keyname)
         if key.does_exist:
             if 'display_filename' in kwargs:
                 return key.generate_url(3600, display_filename=kwargs['display_filename'])
             else:
                 return key.generate_url(3600)
         else:
             #logmessage("Key " + str(keyname) + " did not exist")
             #why not serve right from uploadedpage in this case?
             sys.stderr.write("key " + str(keyname) + " did not exist\n")
             return('about:blank')
     else:
         if extn is None:
             extn = ''
         else:
             extn = '.' + extn
         root = daconfig.get('root', '/')
         fileroot = daconfig.get('fileserver', root)
         if 'display_filename' in kwargs:
             filename = kwargs['display_filename']
         if self.section == 'files':
             if 'page' in kwargs and kwargs['page']:
                 page = re.sub(r'[^0-9]', '', str(kwargs['page']))
                 size = kwargs.get('size', 'page')
                 url = fileroot + 'uploadedpage'
                 if size == 'screen':
                     url += 'screen'
                 url += '/' + str(self.file_number) + '/' + str(page)
             else:
                 if re.search(r'\.', str(filename)):
                     url = fileroot + 'uploadedfile/' + str(self.file_number) + '/' + str(filename)
                 elif extn != '':
                     url = fileroot + 'uploadedfile/' + str(self.file_number) + '/' + str(filename) + extn
                 else:
                     url = fileroot + 'uploadedfile/' + str(self.file_number)
         else:
             sys.stderr.write("section " + section + " was wrong\n")
             url = 'about:blank'
         if use_external and url.startswith('/'):
             url = docassemble.base.functions.get_url_root() + url
         return(url)
Ejemplo n.º 27
0
 def url_for(self, **kwargs):
     if 'ext' in kwargs and kwargs['ext'] is not None:
         extn = kwargs['ext']
         extn = re.sub(r'^\.', '', extn)
     else:
         extn = None
     filename = kwargs.get('filename', self.filename)
     use_external = kwargs.get('_external', False)
     if cloud is not None and not (self.section == 'files' and 'page' in kwargs and kwargs['page']):
         keyname = str(self.section) + '/' + str(self.file_number) + '/' + str(filename)
         page = kwargs.get('page', None)
         if page:
             size = kwargs.get('size', 'page')
             page = re.sub(r'[^0-9]', '', str(page))
             if size == 'screen':
                 keyname += 'screen-' + str(page) + '.png'
             else:
                 keyname += 'page-' + str(page) + '.png'
         elif extn:
             keyname += '.' + extn
         key = cloud.get_key(keyname)
         if key.does_exist:
             if 'display_filename' in kwargs:
                 return key.generate_url(3600, display_filename=kwargs['display_filename'])
             else:
                 return key.generate_url(3600)
         else:
             #logmessage("Key " + str(keyname) + " did not exist")
             #why not serve right from uploadedpage in this case?
             sys.stderr.write("key " + str(keyname) + " did not exist\n")
             return('about:blank')
     else:
         if extn is None:
             extn = ''
         else:
             extn = '.' + extn
         root = daconfig.get('root', '/')
         fileroot = daconfig.get('fileserver', root)
         if 'display_filename' in kwargs:
             filename = kwargs['display_filename']
         if self.section == 'files':
             if 'page' in kwargs and kwargs['page']:
                 page = re.sub(r'[^0-9]', '', str(kwargs['page']))
                 size = kwargs.get('size', 'page')
                 url = fileroot + 'uploadedpage'
                 if size == 'screen':
                     url += 'screen'
                 url += '/' + str(self.file_number) + '/' + str(page)
             else:
                 if re.search(r'\.', str(filename)):
                     url = fileroot + 'uploadedfile/' + str(self.file_number) + '/' + str(filename)
                 elif extn != '':
                     url = fileroot + 'uploadedfile/' + str(self.file_number) + '/' + str(filename) + extn
                 else:
                     url = fileroot + 'uploadedfile/' + str(self.file_number)
         else:
             sys.stderr.write("section " + section + " was wrong\n")
             url = 'about:blank'
         if use_external and url.startswith('/'):
             url = docassemble.base.functions.get_url_root() + url
         return(url)