You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2020/04/02 00:47:34 UTC

[GitHub] [beam] tvalentyn commented on a change in pull request #11067: [BEAM-9136]Add licenses for dependencies for Python

tvalentyn commented on a change in pull request #11067: [BEAM-9136]Add licenses for dependencies for Python
URL: https://github.com/apache/beam/pull/11067#discussion_r401988525
 
 

 ##########
 File path: sdks/python/container/license_scripts/pull_licenses_py.py
 ##########
 @@ -0,0 +1,143 @@
+#
+# 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.
+The script is executed within Docker.
+"""
+import json
+import os
+import shutil
+import subprocess
+import sys
+import tempfile
+import traceback
+import yaml
+
+from future.moves.urllib.request import urlopen
+from tenacity import retry
+from tenacity import stop_after_attempt
+from tenacity import wait_exponential
+
+LICENSE_DIR = '/opt/apache/beam/third_party_licenses'
+
+
+def run_bash_command(command):
+  return subprocess.check_output(command.split()).decode('utf-8')
+
+
+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'].lower()
+  dest_dir = '/'.join([LICENSE_DIR, name])
+  try:
+    os.mkdir(dest_dir)
+    shutil.copy(source_license_file, dest_dir + '/LICENSE')
+    print(
+        'Successfully pulled license for {dep} with pip-licenses.'.format(
+            dep=name))
+    return True
+  except Exception as e:
+    print(
+        'Failed to copy from {source} to {dest}'.format(
+            source=source_license_file, dest=dest_dir + '/LICENSE'))
+    traceback.print_exc()
+
+
+@retry(wait=wait_exponential(multiplier=1, min=30, max=180))
+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:
+    config = configs[dep]
+    dest_dir = '/'.join([LICENSE_DIR, dep])
+    cur_temp_dir = tempfile.mkdtemp()
+
+    try:
+      if config['license'] == 'skip':
+        print('Skip pulling license for ', dep)
+      else:
+        url_read = urlopen(config['license'])
+        with open(cur_temp_dir + '/LICENSE', 'wb') as temp_write:
+          shutil.copyfileobj(url_read, temp_write, length=1 << 20)
+        print(
+            'Successfully pulled license for {dep} from {url}.'.format(
+                dep=dep, url=config['license']))
+
+      # notice is optional.
+      if 'notice' in config:
+        url_read = urlopen(config['notice'])
+        with open(cur_temp_dir + '/NOTICE', 'wb') as temp_write:
+          shutil.copyfileobj(url_read, temp_write, length=1 << 20)
+
+      shutil.copytree(cur_temp_dir, dest_dir)
+      shutil.rmtree(cur_temp_dir)
+      return True
+    except Exception as e:
+      shutil.rmtree(cur_temp_dir)
 
 Review comment:
   followed up offline - you don't need to move `return` statement into `finally` block.

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services