Example #1
0
def workspace(root_path):
    u''' 源库所在工作区 '''
    if StaticPackage.get_root(root_path):
        ui.msg(Workspace.get_workspace(root_path))
    else:
        ui.msg(u'不是一个源库')
        return 1
Example #2
0
def serve(workspace_path, fastcgi = False, port = 8080, debug = False, noload = False, hg = False, hg_port = 8000):
    u''' 启动一个可实时编译的静态服务器

请指定工作区路径'''

    if Workspace.is_root(workspace_path):
        workspace = Workspace(os.path.realpath(workspace_path))
        if not noload:
            load(workspace = workspace)
    else:
        ui.error(u'工作区无效');
        workspace = None

    def print_request(environ, start_response):
        ''' 输出fastcgi本次请求的相关信息 '''

        import cgi
        start_response('200 OK', [('Content-Type', 'text/html')])
        yield '<html><head><title>Hello World!</title></head>\n' \
              '<body>\n' \
              '<p>Hello World!</p>\n' \
              '<table border="1">'
        names = environ.keys()
        names.sort()
        for name in names:
            yield '<tr><td>%s</td><td>%s</td></tr>\n' % (
                name, cgi.escape(`environ[name]`))

        form = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ,
                                keep_blank_values=1)
        if form.list:
            yield '<tr><th colspan="2">Form data</th></tr>'

        for field in form.list:
            yield '<tr><td>%s</td><td>%s</td></tr>\n' % (
                field.name, field.value)

        yield '</table>\n' \
              '</body></html>\n'

    def listen(environ, start_response):
        ''' 监听请求 '''
        if environ['DOCUMENT_URI'].endswith('/net.test'):
            return print_request(environ, start_response)

        DEBUG = debug

        filename = os.path.realpath(environ['REQUEST_FILENAME'])
        url = environ['DOCUMENT_URI']

        force = False # 是否强制重新编译
        # 没有 referer 时强制重新编译
        if not 'HTTP_REFERER' in environ.keys():
            force = True

        try:
            publish_path, root_path = StaticPackage.get_roots(filename, workspace = workspace)
        except PackageNotFoundException, e:
            ui.error(u'%s package not found' % e.url)
        else:
Example #3
0
def get(workspace, url):
    u''' 从公共代码库获取源库

 当前目录会被作为工作区,所有获取到的源库代码都会放到此工作区内
    '''

    if workspace.__class__ == str:
        try:
            workspace = Workspace(workspace)
        except ConfigError as e:
            ui.error(u'config error %s' % e.path)
            return 1

    load(workspace)

    try:
        packages = workspace.fetch_packages(url)
    except PackageNotFoundException, e:
        ui.error('%s package not found' % e.url)
        return 1
Example #4
0
def load(workspace):
    u''' 加载本地工作区 '''

    if workspace.__class__ == str:
        workspace_path = Workspace.get_workspace(workspace)
        if not workspace_path:
            workspace_path = workspace

        try:
            workspace = Workspace(workspace_path)
        except ConfigError as e:
            ui.error(u'config error %s' % e.path)
            return 1

    old_count = len(workspace.local_packages)
    workspace.load()
    added_count = len(workspace.local_packages) - old_count

    if len(workspace.useless_packages):
        for package in workspace.useless_packages:
            ui.msg(u'删除无用package %s' % package)

    ui.msg(u'已加入 %s 个源库' % added_count)
Example #5
0
def packages(workspace_path, show_url = False):
    u''' 本工作区中所有源库 '''

    if os.path.isfile(workspace_path):
        workspace_path = os.path.dirname(workspace_path)

    # 有可能不是workspace跟路径,而是某个子路径
    workspace_path = Workspace.get_workspace(workspace_path)
    if not workspace_path:
        ui.error(u'没有工作区')
        return 1
    else:
        workspace = Workspace(workspace_path)
        if show_url:
            for url in workspace.url_packages.keys():
                ui.msg(url)
        else:
            for local_path in workspace.local_packages.keys():
                ui.msg(os.path.realpath(os.path.join(workspace.root, local_path)))
Example #6
0
def init(root_path, publish_path = None, force = False):
    u''' 初始化一个新的库
    
初始化一个新的库,建立template-config.xml配置文件及常用的目录,如果指定了 -p 参数,还可以自动建立与发布目录的连接'''

    # 确保不要init了一个工作区
    if Workspace.get_workspace(root_path) == root_path:
        ui.error(u'工作区不能被初始化')
        return 1

    # 确保不要init了一个publish库
    if StaticPackage.get_publish(root_path):
        ui.error(u'发布目录不能被初始化')
        return 1

    ui.msg(u'初始化%s' % root_path)
    try:
        StaticPackage.init(root_path)
        ui.msg(u'创建配置文件')

    except PackageExistsException:
        ui.error(u'已经存在')
        return 1

    pathnames = ['test', 'doc', 'src', 'lib', 'res']
    for name in pathnames:
        path = os.path.join(root_path, name)
        if not os.path.exists(path):
            os.makedirs(path)
            ui.msg(u'生成默认目录 %s' % name)

    workspace_path = Workspace.get_workspace(root_path)
    if not workspace_path:
        ui.msg(u'没有工作区,请参照 opm help load')
    else:
        workspace = Workspace(workspace_path)

        if not workspace.has_package(root_path):
            workspace.add_package(root_path)
            ui.msg(u'加入本地工作区')
        else:
            ui.msg(u'本地工作区中已存在')

    ui.msg(u'成功!')

    if publish_path:
        link(root_path, publish_path, force = force)