[ 
https://issues.apache.org/jira/browse/BEAM-9136?focusedWorklogId=400496&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-400496
 ]

ASF GitHub Bot logged work on BEAM-9136:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 10/Mar/20 01:09
            Start Date: 10/Mar/20 01:09
    Worklog Time Spent: 10m 
      Work Description: tvalentyn commented on pull request #11067: 
[BEAM-9136]Add licenses for dependencies
URL: https://github.com/apache/beam/pull/11067#discussion_r390011284
 
 

 ##########
 File path: licenses/scripts/pull_licenses_py.py
 ##########
 @@ -0,0 +1,157 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+"""A script to pull licenses for Python.
+"""
+import json
+import os
+import shutil
+import subprocess
+import sys
+import yaml
+
+from tenacity import retry
+from tenacity import stop_after_attempt
+
+
+def run_bash_command(command):
+  process = subprocess.Popen(command.split(),
+                             stdout=subprocess.PIPE,
+                             stderr=subprocess.PIPE)
+  result, error = process.communicate()
+  if error:
+    raise RuntimeError('Error occurred when running a bash command.',
+                       'command: ', command, 'error message: ',
+                       error.decode('utf-8'))
+  return result.decode('utf-8')
+
+
+try:
+  import wget
+except:
+  command = 'pip install wget --no-cache-dir'
+  run_bash_command(command)
+  import wget
+
+
+def install_pip_licenses():
+  command = 'pip install pip-licenses --no-cache-dir'
+  run_bash_command(command)
+
+
+def run_pip_licenses():
+  command = 'pip-licenses --with-license-file --format=json'
+  dependencies = run_bash_command(command)
+  return json.loads(dependencies)
+
+
+@retry(stop=stop_after_attempt(3))
+def copy_license_files(dep):
+  source_license_file = dep['LicenseFile']
+  if source_license_file.lower() == 'unknown':
+    return False
+  name = dep['Name']
+  dest_dir = '/'.join([license_dir, name.lower()])
+  try:
+    if not os.path.isdir(dest_dir):
+      os.mkdir(dest_dir)
+    shutil.copy(source_license_file, dest_dir + '/LICENSE')
+    return True
+  except Exception as e:
+    print(e)
+    return False
+
+
+@retry(stop=stop_after_attempt(3))
+def pull_from_url(dep, configs):
+  '''
+  :param dep: name of a dependency
+  :param configs: a dict from dep_urls_py.yaml
+  :return: boolean
+
+  It downloads files form urls to a temp directory first in order to avoid
+  to deal with any temp files. It helps keep clean final directory.
+  '''
+  if dep in configs.keys():
+    config = configs[dep]
+    dest_dir = '/'.join([license_dir, dep])
+    cur_temp_dir = 'temp_license_' + dep
+    os.mkdir(cur_temp_dir)
+    try:
+      is_file_available = False
+      # license is required, but not all dependencies have license.
+      # In case we have to skip, print out a message.
+      if config['license'] != 'skip':
+        wget.download(config['license'], cur_temp_dir + '/LICENSE')
+        is_file_available = True
+      # notice is optional.
+      if 'notice' in config:
+        wget.download(config['notice'], cur_temp_dir + '/NOTICE')
+        is_file_available = True
+      # copy from temp dir to final dir only when either file is abailable.
+      if is_file_available:
+        if os.path.isdir(dest_dir):
+          shutil.rmtree(dest_dir)
+        shutil.copytree(cur_temp_dir, dest_dir)
+      result = True
+    except Exception as e:
+      print('Error occurred when pull license from url.', 'dependency =',
+            dep, 'url =', config, 'error = ', e.decode('utf-8'))
+      result = False
+    finally:
+      shutil.rmtree(cur_temp_dir)
+      return result
+  else:
+    return False
+
+
+if __name__ == "__main__":
+  cur_dir = os.getcwd()
+  if cur_dir.split('/')[-1] != 'beam':
+    raise RuntimeError('This script should run from ~/beam directory.')
+  license_dir = os.getcwd() + '/licenses/python'
+  no_licenses = []
+
+  with open('licenses/scripts/dep_urls_py.yaml') as file:
+    dep_config = yaml.load(file)
 
 Review comment:
   This line generates a warning: `calling yaml.load() without Loader=... is 
deprecated, as the default Loader is unsafe`
 
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Issue Time Tracking
-------------------

    Worklog Id:     (was: 400496)
    Time Spent: 1h 10m  (was: 1h)

> Add LICENSES and NOTICES to docker images
> -----------------------------------------
>
>                 Key: BEAM-9136
>                 URL: https://issues.apache.org/jira/browse/BEAM-9136
>             Project: Beam
>          Issue Type: Task
>          Components: build-system
>            Reporter: Hannah Jiang
>            Assignee: Hannah Jiang
>            Priority: Major
>          Time Spent: 1h 10m
>  Remaining Estimate: 0h
>
> Scan dependencies and add licenses and notices of the dependencies to SDK 
> docker images.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to