
from jupyter_core.paths import jupyter_data_dir
import subprocess
import os
import errno
import stat

c = get_config()
c.NotebookApp.ip = '*'
c.NotebookApp.port = 8888
c.NotebookApp.open_browser = False

# Generate a self-signed certificate
if 'GEN_CERT' in os.environ:
    dir_name = jupyter_data_dir()
    pem_file = os.path.join(dir_name, 'notebook.pem')
    try:
        os.makedirs(dir_name)
    except OSError as exc:  # Python >2.5
        if exc.errno == errno.EEXIST and os.path.isdir(dir_name):
            pass
        else:
            raise
    # Generate a certificate if one doesn't exist on disk
    subprocess.check_call(['openssl', 'req', '-new',
                           '-newkey', 'rsa:2048',
                           '-days', '365',
                           '-nodes', '-x509',
                           '-subj', '/C=XX/ST=XX/L=XX/O=generated/CN=generated',
                           '-keyout', pem_file,
                           '-out', pem_file])
    # Restrict access to the file
    os.chmod(pem_file, stat.S_IRUSR | stat.S_IWUSR)
    c.NotebookApp.certfile = pem_file


from jupyter_core.paths import jupyter_data_dir
import subprocess
import os
import os.path
import errno
import stat

c = get_config()
c.NotebookApp.ip = '*'
c.NotebookApp.port = 8888
c.NotebookApp.open_browser = False

# Generate a self-signed certificate
if 'GEN_CERT' in os.environ:
    dir_name = jupyter_data_dir()
    pem_file = os.path.join(dir_name, 'notebook.pem')
    if not os.path.isfile(pem_file):
        try:
            os.makedirs(dir_name)
        except OSError as exc:  # Python >2.5
            if exc.errno == errno.EEXIST and os.path.isdir(dir_name):
                pass
            else:
                raise
        # Generate a certificate if one doesn't exist on disk
        subprocess.check_call(['openssl', 'req', '-new',
                               '-newkey', 'rsa:2048',
                               '-days', '365',
                               '-nodes', '-x509',
                               '-subj', '/C=XX/ST=XX/L=XX/O=generated/CN=generated',
                               '-keyout', pem_file,
                               '-out', pem_file])
        # Restrict access to the file
        os.chmod(pem_file, stat.S_IRUSR | stat.S_IWUSR)
    c.NotebookApp.certfile = pem_file

Fix certificate regenerating each startup
Fix certificate regenerating each startup

  
  from jupyter_core.paths import jupyter_data_dir
  import subprocess
  import os
+ import os.path
  import errno
  import stat
  
  c = get_config()
  c.NotebookApp.ip = '*'
  c.NotebookApp.port = 8888
  c.NotebookApp.open_browser = False
  
  # Generate a self-signed certificate
  if 'GEN_CERT' in os.environ:
      dir_name = jupyter_data_dir()
      pem_file = os.path.join(dir_name, 'notebook.pem')
+     if not os.path.isfile(pem_file):
-     try:
+         try:
-         os.makedirs(dir_name)
+             os.makedirs(dir_name)
-     except OSError as exc:  # Python >2.5
+         except OSError as exc:  # Python >2.5
-         if exc.errno == errno.EEXIST and os.path.isdir(dir_name):
+             if exc.errno == errno.EEXIST and os.path.isdir(dir_name):
-             pass
+                 pass
-         else:
+             else:
-             raise
+                 raise
-     # Generate a certificate if one doesn't exist on disk
+         # Generate a certificate if one doesn't exist on disk
-     subprocess.check_call(['openssl', 'req', '-new',
+         subprocess.check_call(['openssl', 'req', '-new',
-                            '-newkey', 'rsa:2048',
+                                '-newkey', 'rsa:2048',
-                            '-days', '365',
+                                '-days', '365',
-                            '-nodes', '-x509',
+                                '-nodes', '-x509',
-                            '-subj', '/C=XX/ST=XX/L=XX/O=generated/CN=generated',
+                                '-subj', '/C=XX/ST=XX/L=XX/O=generated/CN=generated',
-                            '-keyout', pem_file,
+                                '-keyout', pem_file,
-                            '-out', pem_file])
+                                '-out', pem_file])
-     # Restrict access to the file
+         # Restrict access to the file
-     os.chmod(pem_file, stat.S_IRUSR | stat.S_IWUSR)
+         os.chmod(pem_file, stat.S_IRUSR | stat.S_IWUSR)
      c.NotebookApp.certfile = pem_file
  
Fix certificate regenerating each startup
## Code Before:

from jupyter_core.paths import jupyter_data_dir
import subprocess
import os
import errno
import stat

c = get_config()
c.NotebookApp.ip = '*'
c.NotebookApp.port = 8888
c.NotebookApp.open_browser = False

# Generate a self-signed certificate
if 'GEN_CERT' in os.environ:
    dir_name = jupyter_data_dir()
    pem_file = os.path.join(dir_name, 'notebook.pem')
    try:
        os.makedirs(dir_name)
    except OSError as exc:  # Python >2.5
        if exc.errno == errno.EEXIST and os.path.isdir(dir_name):
            pass
        else:
            raise
    # Generate a certificate if one doesn't exist on disk
    subprocess.check_call(['openssl', 'req', '-new',
                           '-newkey', 'rsa:2048',
                           '-days', '365',
                           '-nodes', '-x509',
                           '-subj', '/C=XX/ST=XX/L=XX/O=generated/CN=generated',
                           '-keyout', pem_file,
                           '-out', pem_file])
    # Restrict access to the file
    os.chmod(pem_file, stat.S_IRUSR | stat.S_IWUSR)
    c.NotebookApp.certfile = pem_file

## Instruction:
Fix certificate regenerating each startup
## Code After:

from jupyter_core.paths import jupyter_data_dir
import subprocess
import os
import os.path
import errno
import stat

c = get_config()
c.NotebookApp.ip = '*'
c.NotebookApp.port = 8888
c.NotebookApp.open_browser = False

# Generate a self-signed certificate
if 'GEN_CERT' in os.environ:
    dir_name = jupyter_data_dir()
    pem_file = os.path.join(dir_name, 'notebook.pem')
    if not os.path.isfile(pem_file):
        try:
            os.makedirs(dir_name)
        except OSError as exc:  # Python >2.5
            if exc.errno == errno.EEXIST and os.path.isdir(dir_name):
                pass
            else:
                raise
        # Generate a certificate if one doesn't exist on disk
        subprocess.check_call(['openssl', 'req', '-new',
                               '-newkey', 'rsa:2048',
                               '-days', '365',
                               '-nodes', '-x509',
                               '-subj', '/C=XX/ST=XX/L=XX/O=generated/CN=generated',
                               '-keyout', pem_file,
                               '-out', pem_file])
        # Restrict access to the file
        os.chmod(pem_file, stat.S_IRUSR | stat.S_IWUSR)
    c.NotebookApp.certfile = pem_file
